package com.ruoyi.form.service.impl;

import java.util.List;
import java.util.Map;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.db.domain.FormInfo;
import com.ruoyi.db.mapper.FormInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.db.mapper.FormInfoPushRecordMapper;
import com.ruoyi.db.domain.FormInfoPushRecord;
import com.ruoyi.form.service.IFormInfoPushRecordService;
import com.ruoyi.common.core.text.Convert;

import javax.annotation.Resource;

/**
 * api手动推送记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-02-27
 */
@Service
@DataSource(DataSourceType.SLAVE)
public class FormInfoPushRecordServiceImpl implements IFormInfoPushRecordService 
{
    @Resource
    private FormInfoPushRecordMapper formInfoPushRecordMapper;
    @Resource
    FormInfoMapper formInfoMapper;

    /**
     * 查询api手动推送记录
     * 
     * @param id api手动推送记录主键
     * @return api手动推送记录
     */
    @Override
    public FormInfoPushRecord selectFormInfoPushRecordById(Long id)
    {
        return formInfoPushRecordMapper.selectFormInfoPushRecordById(id);
    }

    /**
     * 查询api手动推送记录列表
     * 
     * @param formInfoPushRecord api手动推送记录
     * @return api手动推送记录
     */
    @Override
    public List<FormInfoPushRecord> selectFormInfoPushRecordList(FormInfoPushRecord formInfoPushRecord)
    {
        return formInfoPushRecordMapper.selectFormInfoPushRecordList(formInfoPushRecord);
    }

    @Override
    public List<FormInfoPushRecord> selectPushTaskPushRecordList(String nowDate, String nowTime) {
        return formInfoPushRecordMapper.selectPushTaskPushRecordList(nowDate, nowTime);
    }

    /**
     * 查询api手动推送记录列表
     *
     * @param formInfoPushRecord api手动推送记录
     * @return api手动推送记录
     */
    @Override
    public List<Map<String, Object>> selectFormInfoPushRecordListMap(FormInfoPushRecord formInfoPushRecord)
    {
        return formInfoPushRecordMapper.selectFormInfoPushRecordListMap(formInfoPushRecord);
    }
  /**
     * 新增api手动推送记录
     * 
     * @param formInfoPushRecord api手动推送记录
     * @return 结果
     */
    @Override
    public int insertFormInfoPushRecord(FormInfoPushRecord formInfoPushRecord)
    {

        FormInfo formInfo = formInfoMapper.selectFormInfoById(String.valueOf(formInfoPushRecord.getDataId()));
        formInfoPushRecord.setDataName(formInfo.getChannelName());

        SysUser sysUser = ShiroUtils.getSysUser();
        formInfoPushRecord.setOperatorId(String.valueOf(sysUser.getUserId()));
        formInfoPushRecord.setOperatorName(sysUser.getUserName());

        if (StrUtil.isNotEmpty(formInfoPushRecord.getFilterZhima())){
            List<String> split = StrUtil.split(formInfoPushRecord.getFilterZhima(), ",");
            formInfoPushRecord.setFilterZhima(JSON.toJSONString(split));
        }

        formInfoPushRecord.setCreateTime(DateUtils.getNowDate());
        return formInfoPushRecordMapper.insertFormInfoPushRecord(formInfoPushRecord);
    }

    /**
     * 修改api手动推送记录
     * 
     * @param formInfoPushRecord api手动推送记录
     * @return 结果
     */
    @Override
    public int updateFormInfoPushRecord(FormInfoPushRecord formInfoPushRecord)
    {

        if (StrUtil.isNotEmpty(formInfoPushRecord.getFilterZhima())){
            List<String> split = StrUtil.split(formInfoPushRecord.getFilterZhima(), ",");
            formInfoPushRecord.setFilterZhima(JSON.toJSONString(split));
        }

        formInfoPushRecord.setUpdateTime(DateUtils.getNowDate());
        return formInfoPushRecordMapper.updateFormInfoPushRecord(formInfoPushRecord);
    }

    /**
     * 批量删除api手动推送记录
     * 
     * @param ids 需要删除的api手动推送记录主键
     * @return 结果
     */
    @Override
    public int deleteFormInfoPushRecordByIds(String ids)
    {
        return formInfoPushRecordMapper.deleteFormInfoPushRecordByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除api手动推送记录信息
     * 
     * @param id api手动推送记录主键
     * @return 结果
     */
    @Override
    public int deleteFormInfoPushRecordById(Long id)
    {
        return formInfoPushRecordMapper.deleteFormInfoPushRecordById(id);
    }
}
